/**
 * Copyright (C) 2009 KingCore Science & Technology CO,.LTD. All rights reserved.
 * XNS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Copyright (C) 2002-2009 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * This software is the confidential and proprietary information of
 * WuZeven, Personal. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with WuZeven.
 */
package com.kingcore.director.core.ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
public class PagingNavigatePanel extends JFrame implements MouseListener,ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public final  String[] columnHeaders = {"ID(编号)","Nmae(姓名)","Sex(性别)" };
	JTable ab;
	JLabel ab1,ab2,ab3,ab4;
	JTextField test1,test2,test3,test01,test02,test03,test04;
	JButton bu,bu1,bu01,bu02,bu03,bu04;
	JScrollPane jsp;
	public String[][] str = null;
	DbBean dbBean = new DbBean();
	int y =0;
	public PagingNavigatePanel(){
		dbBean.sqlCont();
		setTitle("------------------");
		setBounds(100,100,500,400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null);
		bu  = new JButton("下一页");
		bu.setBounds(415,10,75,30);
		bu.addActionListener(this);
		bu1  = new JButton("上一页");
		bu1.setBounds(415,50,75,30);
		bu1.addActionListener(this);
		DbBean dbBean = new DbBean();
		str = dbBean.selectSql(3,y);
		AbstractTableModel tc = new AbstractTableModel(){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			public String getColumnName(int col){
				return columnHeaders[col];
			}
			public int getColumnCount() {
				return str[0].length;
			}
			public int getRowCount() {
				return str.length;
			}
			public Object getValueAt(int row,int column){
				return str[row][column];
			}
		};

		ab = new JTable(tc);
		jsp = new JScrollPane(ab);
		jsp.setBounds(10,10,400, 80);
		ab.addMouseListener(this);
		test1 = new JTextField(20);
		test1.setBounds(20,100,100,35);
		test2 = new JTextField(20);
		test2.setBounds(170,100,100,35);
		test3 = new JTextField(20);
		test3.setBounds(320,100,100,35);
		add(test1);
		add(bu);
		add(bu1);
		add(test2);
		add(test3); 
		add(jsp); 
		setVisible(true);
	}
	public static void main(String[] args) {
		new PagingNavigatePanel();
	}
	public void mouseClicked(MouseEvent e) {
		test1.setText((String)ab.getValueAt(ab.getSelectedRow(),0));
		test2.setText((String)ab.getValueAt(ab.getSelectedRow(),1));
		test3.setText((String)ab.getValueAt(ab.getSelectedRow(),2));
	}
	public void actionPerformed(ActionEvent e) {
		int x = dbBean.sqlCont();
		if(e.getActionCommand().equals("下一页")){
			y++;
			if(y>x/3){
				JOptionPane.showMessageDialog(this,"没有下一页了");
				y--;
			}else{
				str = dbBean.selectSql(3, y);
				ab.updateUI();
			}
		}else if(e.getActionCommand().equals("上一页")){
			y--;
			if(y<0){
				JOptionPane.showMessageDialog(this,"没有上一页了");
				y++;
			}else{
				str = dbBean.selectSql(3, y);
				ab.updateUI();
			}
		}
	}
	
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	} 
	class DbBean{
		public   String[][] dataValues =null;
		public   Connection connSql(){
			Connection conn = null;
			try {
				//		   Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
				//		   conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=strBT","sa","") ;
				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				conn = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:/Program Files/KingCore/director/data/Director.mdb") ;
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return conn;
		}
		public int sqlCont(){
			int x = 0;
			try {
				Connection conn = connSql();
				String sql = "select* from FMT_FavoriteFiles"; //stu1
				Statement sta = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
				ResultSet re = sta.executeQuery(sql);
				re.last();
				x = re.getRow();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return x;
		}
		public   String[][] selectSql(int x,int y){
			dataValues = new String[x][3];
			int count = 0 ;
			Connection conn = connSql();
			String sql = "select top "+x+" Times,LastAccessed,Typee from FMT_FavoriteFiles where LastAccessed  not in " +
					"( select top "+(y*3+1)+" LastAccessed from FMT_FavoriteFiles order by LastAccessed desc)" +
					"order by LastAccessed desc";//"select top "+x+" * from FMT_FavoriteFiles where LastAccessed  not in (select top "+y*3+" LastAccessed from FMT_FavoriteFiles order by LastAccessed desc )order by LastAccessed desc";
			//sql="select top 3 Times,LastAccessed,Typee from FMT_FavoriteFiles where LastAccessed  not in ( select top 2 LastAccessed from FMT_FavoriteFiles order by LastAccessed desc)order by LastAccessed desc";
			System.out.println(sql);
			try {
				Statement sta = conn.createStatement();
				ResultSet re = sta.executeQuery(sql);
				while(re.next()){
					dataValues[count][0]=String.valueOf((re.getInt(1))); 
					dataValues[count][1]=re.getString(2);
					dataValues[count][2]=re.getString(3); 
					count++;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return dataValues;
		}

	}
}